package com.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.crm.dto.CourseDeleteIdsDto;
import com.crm.entity.SysDictData;
import com.crm.entity.TbCourse;
import com.crm.dto.CoursePageDto;
import com.crm.mapper.CourseMapper;
import com.crm.result.PageResult;
import com.crm.result.Result;
import com.crm.service.CourseService;
import com.crm.vo.TbCourseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 课程分页查询
     * @param coursePageDto
     * @return
     */
    @Override
    public PageResult list(CoursePageDto coursePageDto) {
        //开启分页
        PageHelper.startPage(coursePageDto.getPage(),coursePageDto.getPageSize());
        //查询
        List<TbCourse> list =courseMapper.list(coursePageDto);
        //转
        Page<TbCourse>p =(Page<TbCourse>) list;
        //封装
        return new PageResult<>(p.getTotal(),p.getResult());

    }
    //课程新增
    @Override
    public void save(CoursePageDto coursePageDto) {
        //dto转entity
        TbCourse tbCourse = BeanUtil.copyProperties(coursePageDto,TbCourse.class);
        String code = RandomUtil.randomNumbers(6);
        //填充数据
        tbCourse.setCode(code);
        tbCourse.setCreateTime(LocalDateTime.now());
        tbCourse.setIsDelete(0);
        //添加
        courseMapper.save(tbCourse);
    }
    //删除课程
    @Override
    public void CourseDeleteId(CourseDeleteIdsDto courseDeleteIdsDto) {
        ArrayList<Long> ids = courseDeleteIdsDto.getIds();
        ids.forEach(id->{
            courseMapper.CourseDeleteId(id);
        });
    }
    //根据id查询课程
    @Override
    public TbCourseVo CourseById(Long id) {
        TbCourseVo tbCourseVo=courseMapper.CourseById(id);
        return tbCourseVo;

    }
    //下拉框
    @Override
    public List<SysDictData> subject(String subject) {
        List<SysDictData> list=courseMapper.getSubject(subject);
        return list;
    }
    //修改
    @Override
    public void update(TbCourse tbCourse) {
        courseMapper.update(tbCourse);
    }


}
